package com.hsylient.javase.chapter03;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Slf4j
public class HashSetTest {

    @Test
    public void testHashSetDistinct() {
        Set<String> set = new HashSet<>();
        set.add("中国");
        set.add("民族");
        set.add("台湾");
        set.add("香港");
        set.add("香港");

        log.info("[HashSet测试] 去重后的结果是 {}", set);
    }

    /**
     * 重写equals方法必须重写hashcode方法
     */
    @Test
    public void testHashSet() {
        Student student = new Student();
        student.setName("xx");
        Student student2 = new Student();
        student2.setName("xx");

        Set<Student> studentSet = new HashSet<>();
        studentSet.add(student);
        studentSet.add(student2);
        System.out.println(studentSet);

    }

    @Test
    public void testEntry() {
        Map.Entry<String, String> entry = Map.entry("name", "hsylient");
        String result = String.format("%s:%s", entry.getKey(), entry.getValue());
        System.out.println(result);
    }


    @Data
    public static class Student {
        private String name;
        private Integer age;
        private Integer id;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return Objects.equals(name, student.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }
}
